package com.k2.gulimall.member.service.impl;

import com.k2.common.vo.UserRegisterVo;
import com.k2.gulimall.member.entity.MemberEntity;
import com.k2.gulimall.member.service.MemberLoginLogService;
import com.k2.gulimall.member.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.k2.common.utils.PageUtils;
import com.k2.common.utils.Query;

import com.k2.gulimall.member.dao.MemberStatisticsInfoDao;
import com.k2.gulimall.member.entity.MemberStatisticsInfoEntity;
import com.k2.gulimall.member.service.MemberStatisticsInfoService;


@Service("memberStatisticsInfoService")
public class MemberStatisticsInfoServiceImpl extends ServiceImpl<MemberStatisticsInfoDao, MemberStatisticsInfoEntity> implements MemberStatisticsInfoService {

    @Autowired
    MemberService memberService;
    @Autowired
    MemberLoginLogService memberLoginLogService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<MemberStatisticsInfoEntity> queryWrapper = new QueryWrapper<MemberStatisticsInfoEntity>();
        if (params.get("memberId")!=null&&params.get("memberId").toString().length()>0) {
            queryWrapper.like("member_id", params.get("memberId"));
        }
        if (params.get("consumeAmount")!=null&&params.get("consumeAmount").toString().length()>0) {
            queryWrapper.ge("consume_amount", params.get("consumeAmount"));
        }
        if (params.get("orderCount")!=null&&params.get("orderCount").toString().length()>0) {
            queryWrapper.ge("order_count", params.get("orderCount"));
        }
        IPage<MemberStatisticsInfoEntity> page = this.page(
                new Query<MemberStatisticsInfoEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<MemberStatisticsInfoEntity> getMemberIdToStatisticsInfo(MemberStatisticsInfoEntity memberStatisticsInfoEntity) {
        QueryWrapper<MemberStatisticsInfoEntity> queryWrapper = new QueryWrapper<MemberStatisticsInfoEntity>();
        queryWrapper.or().eq("member_id",memberStatisticsInfoEntity.getMemberId());
        List<MemberStatisticsInfoEntity> memberStatisticsInfoEntityList = this.list(queryWrapper);
        return memberStatisticsInfoEntityList;
    }

    /**
     * 添加会员时，添加会员统计信息
     * @param userRegisterVo
     * @return
     */
    @Override
    public boolean addmemberStatisticsInfo(UserRegisterVo userRegisterVo) {
        try {
            MemberEntity memberEntity = new MemberEntity();
            memberEntity.setUsername(userRegisterVo.getUserName());
            //根据用户名查询添加的用户
            List<MemberEntity> getMember = memberService.getUserName(memberEntity);
            if(getMember.size()<1){
                //通过社交登录id查询用户
                memberEntity = memberService.getUserSocialUid(userRegisterVo.getAccessToken());
            }else{
                memberEntity = getMember.get(0);
            }
            //添加统计信息
            MemberStatisticsInfoEntity memberStatisticsInfo = new MemberStatisticsInfoEntity();
            //用户id
            memberStatisticsInfo.setMemberId(memberEntity.getId());
            //保存统计数据
            this.saveOrUpdate(memberStatisticsInfo);

            //添加登陆记录
            memberLoginLogService.addmemberLoginLog(userRegisterVo);
            return true;
        }catch (Exception e){
            return false;
        }
    }

}